package com.docker.jy.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.docker.chy.domain.DoctorPatient;
import com.docker.chy.mapper.DoctorPatientMapper;
import com.docker.chy.mapper.DoctorRegistationcostMapper;
import com.docker.common.annotation.Anonymous;
import com.docker.common.core.domain.entity.SysUser;
import com.docker.fy.domain.DoctorRegistration;
import com.docker.fy.mapper.DoctorRegistrationMapper;
import com.docker.system.mapper.SysUserMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.docker.common.annotation.Log;
import com.docker.common.core.controller.BaseController;
import com.docker.common.core.domain.AjaxResult;
import com.docker.common.enums.BusinessType;
import com.docker.jy.domain.DoctorAchievement;
import com.docker.jy.service.IDoctorAchievementService;
import com.docker.common.utils.poi.ExcelUtil;
import com.docker.common.core.page.TableDataInfo;

/**
 * 医生业绩统计Controller
 * 
 * @author ruoyi
 * @date 2025-04-15
 */
@RestController
@RequestMapping("/jy/doctorAchievement")
public class DoctorAchievementController extends BaseController
{
    @Autowired
    private IDoctorAchievementService doctorAchievementService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private DoctorPatientMapper doctorPatientMapper;
    @Autowired
    private DoctorRegistrationMapper doctorRegistrationMapper;

    /**
     * 查询医生业绩统计列表
     */
    @PreAuthorize("@ss.hasPermi('jy:doctorAchievement:list')")
    @GetMapping("/list")
    public TableDataInfo list(DoctorAchievement doctorAchievement)
    {
        startPage();
        List<DoctorAchievement> list = doctorAchievementService.selectDoctorAchievementList(doctorAchievement);
        return getDataTable(list);
    }

    /**
     * 导出医生业绩统计列表
     */
    @PreAuthorize("@ss.hasPermi('jy:doctorAchievement:export')")
    @Log(title = "医生业绩统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DoctorAchievement doctorAchievement)
    {
        List<DoctorAchievement> list = doctorAchievementService.selectDoctorAchievementList(doctorAchievement);
        ExcelUtil<DoctorAchievement> util = new ExcelUtil<DoctorAchievement>(DoctorAchievement.class);
        util.exportExcel(response, list, "医生业绩统计数据");
    }

    /**
     * 获取医生业绩统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('jy:doctorAchievement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(doctorAchievementService.selectDoctorAchievementById(id));
    }

    /**
     * 新增医生业绩统计
     */
    @PreAuthorize("@ss.hasPermi('jy:doctorAchievement:add')")
    @Log(title = "医生业绩统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DoctorAchievement doctorAchievement)
    {
        return toAjax(doctorAchievementService.insertDoctorAchievement(doctorAchievement));
    }

    /**
     * 修改医生业绩统计
     */
    @PreAuthorize("@ss.hasPermi('jy:doctorAchievement:edit')")
    @Log(title = "医生业绩统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DoctorAchievement doctorAchievement)
    {
        return toAjax(doctorAchievementService.updateDoctorAchievement(doctorAchievement));
    }

    /**
     * 删除医生业绩统计
     */
    @PreAuthorize("@ss.hasPermi('jy:doctorAchievement:remove')")
    @Log(title = "医生业绩统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(doctorAchievementService.deleteDoctorAchievementByIds(ids));
    }

    //查询doctor_id和totalcost统计列表
    @Anonymous
    @GetMapping("/selectDoctorIdAndTotalcost")
    public AjaxResult selectDoctorIdAndTotalcost(){
        List<Map> list = doctorAchievementService.selectDoctorIdAndTotalcost();
        //通过doctor_id一对一查询医生信息
        for (Map map : list) {
            Long doctorId = ((Integer) map.get("doctor_id")).longValue();
            SysUser doctor = sysUserMapper.selectUserById(doctorId);
            System.out.println(doctor);
            map.put("doctor",doctor);
        }
        return success(list);
    }

    //查询doctor_id和totalcost统计列表
    @Anonymous
    @GetMapping("/selectDoctorIdAndPatientId")
    public AjaxResult selectDoctorIdAndPatientId() {
        List<Map> list = doctorAchievementService.selectDoctorIdAndPatientId();
        for (Map map : list) {
            Object patientIdObj = map.get("patient_id");
            if (patientIdObj != null) {
                Long patientId = ((Integer) patientIdObj).longValue();
                DoctorPatient patient = doctorPatientMapper.selectDoctorPatientById(patientId);
                DoctorRegistration registration = doctorRegistrationMapper.selectDoctorRegistrationById(patientId);
//                System.out.println(map.get("doctor_id"));
//                System.out.println(map.get("patient_id"));
//                System.out.println(registration);
                map.put("patient", patient);
                map.put("registration", registration);
            } else {
                map.put("registration", null);
            }
        }
        //通过patient_id一对一查询
        return success(list);
    }
}
